/**
* Copyright(c) 2019 六六微科技 Co. Ltd.
* All right reserved.
/*
* 权限模块
* @author 小六子
* @date 2019年9月22日
*/
import axios from '../../axios'
import store from '@/store'
import router from '@/router'
import notification from 'ant-design-vue/es/notification'
import { USER_ID, USER_TYPE, INDEX_MAIN_PAGE_PATH } from '@/store/mutation-types'
import { generateIndexRouter } from "@/utils/util"
import Vue from 'vue'

// 保存
export const saveOrUpdate = (data) => {
  return axios({
    url: '/permission/permission/saveOrUpdate',
    method: 'post',
    data
  })
}
// 获取
export const get = (id) => {
  return axios({
    url: '/permission/permission/get',
    method: 'get',
    params: {
      id: id
    }
  })
}
// 删除
export const remove = (id) => {
  return axios({
    url: '/permission/permission/remove',
    method: 'get',
    params: {
      id: id
    }
  })
}
// 批量删除
export const batchRemove = (data) => {
  return axios({
    url: '/permission/permission/remove',
    method: 'post',
    data
  })
}
// 查询
export const query = (data) => {
  return axios({
    url: '/permission/permission/query',
    method: 'post',
    data
  })
}
// 获取权限树
export const getAllOfTree = () => {
  return axios({
    url: '/permission/permission/getAllOfTree',
    method: 'get'
  })
}
//重新加载会员权限
export const reloadPermissions = () => {
  return axios({
      url: '/reloadPermissions',
      method: 'post'
    })
}
//重建权限
export const rebuildPermissions = () => {
  return axios({
      url: '/rebuildPermissions',
      method: 'post'
    })
}

// 处理菜单
export const reloadMenus= (redirect) => {
  return new Promise(resolve => {
    store.dispatch('ReloadAdminMenus').then(res => {
      const menuData = res;
      if (menuData === null || menuData === "" || menuData === undefined) {
        return;
      }
      let constRoutes = [];
      let crs = generateIndexRouter(menuData);
      if(crs){
        for(let cr of crs){
          if(cr.children){
              for(let cc of cr.children){
                 constRoutes.push(cc);
             }
          }
        }
      }
     constRoutes.splice(0,0,crs[0])
      // 添加主界面路由
      store.dispatch('UpdateAppRouter',  { constRoutes }).then(() => {
        // 根据roles权限生成可访问的路由表
        // 动态添加可访问路由表
        router.addRoutes(store.getters.addRouters)
        // const redirect = decodeURIComponent(from.query.redirect || to.path)
        router.push({ path: redirect })
      })
    })
    .catch((e) => {
      console.log(e)
      store.dispatch('Logout').then(() => {
         router.push({ path: '/user/adminLogin' })
      })
    })
  })
}